package com.lancoo.znbkxx.teacher.aibkTestCoach.aibkDialog

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.*
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.lancoo.znbkxx.teacher.R
import kotlinx.android.synthetic.main.aibk_adapter_k_big_image.*
import kotlinx.android.synthetic.main.aibk_adapter_k_big_image.view.*
import kotlinx.android.synthetic.main.aibk_dialog_k_big_image.*
import java.util.*

/**
 * auther: 王国龙
 *  Date：2020.06.28
 * describe:查看大图
 * 案例：https://obs-lgresv56pc-yanyi.obs.cn-east-2.myhuaweicloud.com/lgRs/78e889779fa0449296487b91f16b4ada/41800ea5c2a648e5aacffda073b6962d.gif
 */
class DialogBigImage(
        context: Context?,
        val activity:Activity,
        var imagePath: ArrayList<String>,
        var position: Int
) : Dialog(context!!, R.style.CustomDialog) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.aibk_dialog_k_big_image)

        setDialogTheme()

        //按空白处不能取消动画
        setCanceledOnTouchOutside(false)
        //初始化界面数据
        refreshView()
    }

    private fun setDialogTheme() {
        //设置布局顶部显示
        window!!.setGravity(Gravity.TOP)
        //设置背景透明后设置该属性，可去除dialog边框
        window!!.setBackgroundDrawable(ColorDrawable())
        //设置进入动画
        window!!.setWindowAnimations(R.style.MyDialogScale)

        // 关键代码
        if (window != null) {
//            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
            //透明状态栏
            window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window!!.getDecorView().setPadding(0, 0, 0, 0)
            window!!.getDecorView().setBackgroundColor(Color.WHITE)
            val layoutParams: WindowManager.LayoutParams = window!!.getAttributes()
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                // 延伸显示区域到刘海
                val lp: WindowManager.LayoutParams = window!!.getAttributes()
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                window!!.setAttributes(lp)
                // 设置页面全屏显示
                val decorView: View = window!!.getDecorView()
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            }
            window!!.setAttributes(layoutParams)
        }


    }

    /**
     * 初始化界面控件的显示数据
     */
    private fun refreshView() {

        VpBigImage.setAdapter(object : PagerAdapter() {
            override fun getCount(): Int {
                return imagePath.size
            }

            override fun isViewFromObject(view: View, o: Any): Boolean {
                return view === o
            }

            @SuppressLint("ResourceType")
            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                val view = LayoutInflater.from(activity).inflate(R.layout.aibk_adapter_k_big_image, null)
                view.PvImage.setBackgroundColor(activity.getResources().getColor(R.color.textColor))
                var currentIndex = position+1
                view.TvIndex.text =currentIndex.toString() +"/" +imagePath.size
                val url = imagePath.get(position) //图片的url
                Glide.with(activity)
                        .load(url)
                        .placeholder(R.drawable.aibk_drawable_loading) //加载等待图
                        .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存模式
                        .into(view.PvImage)
                container.addView(view)
                //继续按钮
                BtnClose.setOnClickListener {
                    if (clickDialogListener != null) {
                        clickDialogListener!!.onClickClose()
                    }
                }
                return view
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(`object` as View)
            }
        })
        VpBigImage.setCurrentItem(position, true)
    }

    private var clickDialogListener: onClickDialogListener? = null

    /*
   * 设置答题卡题号点击事件
   * */
    interface onClickDialogListener {
        fun onClickClose()//退出
    }

    fun setClickDialogListener(clickDialogListener: onClickDialogListener?) {
        this.clickDialogListener = clickDialogListener
    }

}